<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>kotlin语法入门 | 月藤的博客</title>
    <meta name="generator" content="VuePress 1.8.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/2.10.0/github-markdown.min.css">
    <meta name="description" content="简单的介绍">
    
    <link rel="preload" href="/assets/css/0.styles.a2e941e0.css" as="style"><link rel="preload" href="/assets/js/app.54ea76a2.js" as="script"><link rel="preload" href="/assets/js/2.c554ece5.js" as="script"><link rel="preload" href="/assets/js/37.61156e90.js" as="script"><link rel="prefetch" href="/assets/js/10.95d63e51.js"><link rel="prefetch" href="/assets/js/11.26e082be.js"><link rel="prefetch" href="/assets/js/12.1f62021c.js"><link rel="prefetch" href="/assets/js/13.9096527c.js"><link rel="prefetch" href="/assets/js/14.86142c70.js"><link rel="prefetch" href="/assets/js/15.1adeebc5.js"><link rel="prefetch" href="/assets/js/16.efcc79e4.js"><link rel="prefetch" href="/assets/js/17.74da5698.js"><link rel="prefetch" href="/assets/js/18.fd808b3a.js"><link rel="prefetch" href="/assets/js/19.8dafe26f.js"><link rel="prefetch" href="/assets/js/20.aed54ab4.js"><link rel="prefetch" href="/assets/js/21.341a5670.js"><link rel="prefetch" href="/assets/js/22.ab8b375c.js"><link rel="prefetch" href="/assets/js/23.28489470.js"><link rel="prefetch" href="/assets/js/24.87aae001.js"><link rel="prefetch" href="/assets/js/25.211ca3bf.js"><link rel="prefetch" href="/assets/js/26.afa4c8f0.js"><link rel="prefetch" href="/assets/js/27.9b98f6f3.js"><link rel="prefetch" href="/assets/js/28.93298733.js"><link rel="prefetch" href="/assets/js/29.e8c038c4.js"><link rel="prefetch" href="/assets/js/3.9d562ae1.js"><link rel="prefetch" href="/assets/js/30.27939f01.js"><link rel="prefetch" href="/assets/js/31.6a9774e8.js"><link rel="prefetch" href="/assets/js/32.f698e967.js"><link rel="prefetch" href="/assets/js/33.5ba4bfb8.js"><link rel="prefetch" href="/assets/js/34.dca90861.js"><link rel="prefetch" href="/assets/js/35.768c1bb2.js"><link rel="prefetch" href="/assets/js/36.6b0a867f.js"><link rel="prefetch" href="/assets/js/38.e390246c.js"><link rel="prefetch" href="/assets/js/39.0fbd0dd1.js"><link rel="prefetch" href="/assets/js/4.e18bc6a8.js"><link rel="prefetch" href="/assets/js/40.4252699c.js"><link rel="prefetch" href="/assets/js/41.2e5b5840.js"><link rel="prefetch" href="/assets/js/42.8fe886c7.js"><link rel="prefetch" href="/assets/js/43.7a4b0851.js"><link rel="prefetch" href="/assets/js/44.93c2313d.js"><link rel="prefetch" href="/assets/js/45.33dcea60.js"><link rel="prefetch" href="/assets/js/46.681fdf10.js"><link rel="prefetch" href="/assets/js/47.a842a141.js"><link rel="prefetch" href="/assets/js/48.9a03ba74.js"><link rel="prefetch" href="/assets/js/49.a50266b1.js"><link rel="prefetch" href="/assets/js/5.dc6a2d8c.js"><link rel="prefetch" href="/assets/js/50.f2a42406.js"><link rel="prefetch" href="/assets/js/51.444cc3d8.js"><link rel="prefetch" href="/assets/js/52.cf2befd8.js"><link rel="prefetch" href="/assets/js/53.fbe609f8.js"><link rel="prefetch" href="/assets/js/54.a43fb514.js"><link rel="prefetch" href="/assets/js/55.d0c11641.js"><link rel="prefetch" href="/assets/js/56.c6f114d9.js"><link rel="prefetch" href="/assets/js/57.cc386420.js"><link rel="prefetch" href="/assets/js/58.e747d0f6.js"><link rel="prefetch" href="/assets/js/6.c1fd48f0.js"><link rel="prefetch" href="/assets/js/7.32b39b92.js"><link rel="prefetch" href="/assets/js/8.e2284671.js"><link rel="prefetch" href="/assets/js/9.8def3992.js">
    <link rel="stylesheet" href="/assets/css/0.styles.a2e941e0.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">月藤的博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="https://ziphold.gitee.io/blog/#/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  旧博客（停止更新）
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://rattonlzh.github.io/homepage/homepage.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  常用网址导航（停止更新）
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Select language" class="dropdown-title"><span class="title">Languages</span> <span class="arrow down"></span></button> <button type="button" aria-label="Select language" class="mobile-dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/知识点总结/kotlin语法总结.html" class="nav-link">
  zh-CN
</a></li><li class="dropdown-item"><!----> <a href="/en/" class="nav-link">
  en-US
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><div class="avatar"><img src="/assets/img/avatar.2b77755b.png" alt srcset></div> <div style="z-index: 999"><iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="298" height="52" src="//music.163.com/outchain/player?type=2&id=28283137&auto=1&height=32"></iframe></div> <nav class="nav-links"><div class="nav-item"><a href="https://ziphold.gitee.io/blog/#/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  旧博客（停止更新）
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://rattonlzh.github.io/homepage/homepage.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  常用网址导航（停止更新）
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Select language" class="dropdown-title"><span class="title">Languages</span> <span class="arrow down"></span></button> <button type="button" aria-label="Select language" class="mobile-dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/知识点总结/kotlin语法总结.html" class="nav-link">
  zh-CN
</a></li><li class="dropdown-item"><!----> <a href="/en/" class="nav-link">
  en-US
</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>随便说说</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>知识点总结</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/知识点总结/git总结.html" class="sidebar-link">git总结</a></li><li><a href="/知识点总结/java单元测试总结.html" class="sidebar-link">java测试</a></li><li><a href="/知识点总结/jquery总结.html" class="sidebar-link">jquery初探</a></li><li><a href="/知识点总结/js总结.html" class="sidebar-link">js易错点</a></li><li><a href="/知识点总结/kotlin语法总结.html" class="active sidebar-link">kotlin语法入门</a></li><li><a href="/知识点总结/latex总结.html" class="sidebar-link">latex常用代码</a></li><li><a href="/知识点总结/PS总结.html" class="sidebar-link">PS总结</a></li><li><a href="/知识点总结/springboot总结.html" class="sidebar-link">springboot进行web开发总结</a></li><li><a href="/知识点总结/thinkphp总结.html" class="sidebar-link">thinkphp入坑</a></li><li><a href="/知识点总结/uml软件建模总结.html" class="sidebar-link">uml软件建模总结</a></li><li><a href="/知识点总结/人工智能总结.html" class="sidebar-link">人工智能总结</a></li><li><a href="/知识点总结/大数据软件工程总结.html" class="sidebar-link">大数据软件工程总结</a></li><li><a href="/知识点总结/常用快捷键总结.html" class="sidebar-link">第一篇博客</a></li><li><a href="/知识点总结/数字图像处理总结.html" class="sidebar-link">数字图像处理知识点</a></li><li><a href="/知识点总结/数据库应用理论总结.html" class="sidebar-link">数据库应用复习</a></li><li><a href="/知识点总结/数据库系统概念总结.html" class="sidebar-link">数据库系统概念总结</a></li><li><a href="/知识点总结/数据挖掘总结.html" class="sidebar-link">数据挖掘总结</a></li><li><a href="/知识点总结/设计模式总结.html" class="sidebar-link">设计模式</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>考研专区</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>读书笔记</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开发资料</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>配置记录</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>思维导图</span> <span class="arrow right"></span></p> <!----></section></li><li><a href="/friend_link.html" class="sidebar-link">友情链接</a></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="kotlin语法入门"><a href="#kotlin语法入门" class="header-anchor">#</a> kotlin语法入门</h1> <p>主函数和打印函数</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Hello world!&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>包声明和包导入和java一样</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">package</span> my<span class="token punctuation">.</span>demo

<span class="token keyword">import</span> kotlin<span class="token punctuation">.</span>text<span class="token punctuation">.</span><span class="token operator">*</span>

<span class="token comment">// ...</span>
</code></pre></div><p>简写抛出异常</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">val</span> s <span class="token operator">=</span> person<span class="token punctuation">.</span>name <span class="token operator">?:</span> <span class="token keyword">throw</span> <span class="token function">IllegalArgumentException</span><span class="token punctuation">(</span><span class="token string">&quot;Name required&quot;</span><span class="token punctuation">)</span>
</code></pre></div><p>函数定义</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token comment">// first</span>
<span class="token keyword">fun</span> <span class="token function">sum</span><span class="token punctuation">(</span>a<span class="token operator">:</span> Int<span class="token punctuation">,</span> b<span class="token operator">:</span> Int<span class="token punctuation">)</span><span class="token operator">:</span> Int <span class="token punctuation">{</span>
    <span class="token keyword">return</span> a <span class="token operator">+</span> b
<span class="token punctuation">}</span>

<span class="token comment">// second</span>
<span class="token keyword">fun</span> <span class="token function">sum</span><span class="token punctuation">(</span>a<span class="token operator">:</span> Int<span class="token punctuation">,</span> b<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">=</span> a <span class="token operator">+</span> b

<span class="token comment">// third</span>
<span class="token keyword">fun</span> <span class="token function">printSum</span><span class="token punctuation">(</span>a<span class="token operator">:</span> Int<span class="token punctuation">,</span> b<span class="token operator">:</span> Int<span class="token punctuation">)</span><span class="token operator">:</span> Unit <span class="token punctuation">{</span>
    <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;sum of <span class="token interpolation variable">$a</span> and <span class="token interpolation variable">$b</span> is <span class="token interpolation"><span class="token delimiter variable">${</span>a <span class="token operator">+</span> b<span class="token delimiter variable">}</span></span>&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">// fourth</span>
<span class="token keyword">fun</span> <span class="token function">printSum</span><span class="token punctuation">(</span>a<span class="token operator">:</span> Int<span class="token punctuation">,</span> b<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;sum of <span class="token interpolation variable">$a</span> and <span class="token interpolation variable">$b</span> is <span class="token interpolation"><span class="token delimiter variable">${</span>a <span class="token operator">+</span> b<span class="token delimiter variable">}</span></span>&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>只读变量</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">val</span> a<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">1</span>  <span class="token comment">// immediate assignment</span>
<span class="token keyword">val</span> b <span class="token operator">=</span> <span class="token number">2</span>   <span class="token comment">// `Int` type is inferred</span>
<span class="token keyword">val</span> c<span class="token operator">:</span> Int  <span class="token comment">// Type required when no initializer is provided</span>
c <span class="token operator">=</span> <span class="token number">3</span>     
</code></pre></div><p>字符串替换</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">val</span> s2 <span class="token operator">=</span> <span class="token string">&quot;${s1.replace(&quot;</span><span class="token keyword">is</span><span class="token string">&quot;, &quot;</span>was<span class="token string">&quot;)}, but now is <span class="token interpolation variable">$a</span>&quot;</span>
</code></pre></div><p>if-else结构与java一致，但支持range</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token keyword">in</span> <span class="token number">1</span><span class="token operator">..</span>y<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;fits in range&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>多分支结构when，类似java的switch</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">fun</span> <span class="token function">describe</span><span class="token punctuation">(</span>obj<span class="token operator">:</span> Any<span class="token punctuation">)</span><span class="token operator">:</span> String <span class="token operator">=</span>
    <span class="token keyword">when</span> <span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token number">1</span>          <span class="token operator">-&gt;</span> <span class="token string">&quot;One&quot;</span>
        <span class="token string">&quot;Hello&quot;</span>    <span class="token operator">-&gt;</span> <span class="token string">&quot;Greeting&quot;</span>
        <span class="token keyword">is</span> Long    <span class="token operator">-&gt;</span> <span class="token string">&quot;Long&quot;</span>
        <span class="token operator">!</span><span class="token keyword">is</span> String <span class="token operator">-&gt;</span> <span class="token string">&quot;Not a string&quot;</span>
        <span class="token keyword">else</span>       <span class="token operator">-&gt;</span> <span class="token string">&quot;Unknown&quot;</span>
    <span class="token punctuation">}</span>

<span class="token comment">// another</span>
<span class="token comment">//是否在collections中</span>
<span class="token keyword">when</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;orange&quot;</span> <span class="token keyword">in</span> items <span class="token operator">-&gt;</span> <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;juicy&quot;</span><span class="token punctuation">)</span>
    <span class="token string">&quot;apple&quot;</span> <span class="token keyword">in</span> items <span class="token operator">-&gt;</span> <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;apple is fine too&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>三元条件表达式</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">fun</span> <span class="token function">maxOf</span><span class="token punctuation">(</span>a<span class="token operator">:</span> Int<span class="token punctuation">,</span> b<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>a <span class="token operator">&gt;</span> b<span class="token punctuation">)</span> a <span class="token keyword">else</span> b
</code></pre></div><p>可选null</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">fun</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>str<span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token operator">:</span> Int<span class="token operator">?</span> <span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
<span class="token punctuation">}</span>
</code></pre></div><p>类型检查</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">fun</span> <span class="token function">getStringLength</span><span class="token punctuation">(</span>obj<span class="token operator">:</span> Any<span class="token punctuation">)</span><span class="token operator">:</span> Int<span class="token operator">?</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>obj <span class="token operator">!</span><span class="token keyword">is</span> String<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token keyword">null</span>

    <span class="token comment">// `obj` is automatically cast to `String` in this branch</span>
    <span class="token keyword">return</span> obj<span class="token punctuation">.</span>length
<span class="token punctuation">}</span>
</code></pre></div><p>for循环</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">val</span> items <span class="token operator">=</span> <span class="token function">listOf</span><span class="token punctuation">(</span><span class="token string">&quot;apple&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;banana&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;kiwifruit&quot;</span><span class="token punctuation">)</span>
<span class="token comment">// in</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span>item <span class="token keyword">in</span> items<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">println</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">// step</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span>x <span class="token keyword">in</span> <span class="token number">1</span><span class="token operator">..</span><span class="token number">10</span> step <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">print</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">//reverse</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span>x <span class="token keyword">in</span> <span class="token number">9</span> downTo <span class="token number">0</span> step <span class="token number">3</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">print</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>while循环与java一致</p> <p>迭代器</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">val</span> numbers <span class="token operator">=</span> <span class="token function">mutableListOf</span><span class="token punctuation">(</span><span class="token string">&quot;one&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;four&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;four&quot;</span><span class="token punctuation">)</span> 
<span class="token keyword">val</span> mutableListIterator <span class="token operator">=</span> numbers<span class="token punctuation">.</span><span class="token function">listIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
mutableListIterator<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
mutableListIterator<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;two&quot;</span><span class="token punctuation">)</span>
mutableListIterator<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
mutableListIterator<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">&quot;three&quot;</span><span class="token punctuation">)</span>   
<span class="token function">println</span><span class="token punctuation">(</span>numbers<span class="token punctuation">)</span>
</code></pre></div><p>默认it</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">val</span> fruits <span class="token operator">=</span> <span class="token function">listOf</span><span class="token punctuation">(</span><span class="token string">&quot;banana&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;avocado&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;apple&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;kiwifruit&quot;</span><span class="token punctuation">)</span>
fruits
  <span class="token punctuation">.</span><span class="token function">filter</span> <span class="token punctuation">{</span> it<span class="token punctuation">.</span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
  <span class="token punctuation">.</span><span class="token function">sortedBy</span> <span class="token punctuation">{</span> it <span class="token punctuation">}</span>
  <span class="token punctuation">.</span><span class="token function">map</span> <span class="token punctuation">{</span> it<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
  <span class="token punctuation">.</span><span class="token function">forEach</span> <span class="token punctuation">{</span> <span class="token function">println</span><span class="token punctuation">(</span>it<span class="token punctuation">)</span> <span class="token punctuation">}</span>
</code></pre></div><p>类声明和构造方法</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">class</span> <span class="token function">InitOrderDemo</span><span class="token punctuation">(</span>name<span class="token operator">:</span> String<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">val</span> firstProperty <span class="token operator">=</span> <span class="token string">&quot;First property: <span class="token interpolation variable">$name</span>&quot;</span><span class="token punctuation">.</span><span class="token function">also</span><span class="token punctuation">(</span><span class="token operator">::</span>println<span class="token punctuation">)</span>
    
    <span class="token keyword">init</span> <span class="token punctuation">{</span>
        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;First initializer block that prints <span class="token interpolation"><span class="token delimiter variable">${</span>name<span class="token delimiter variable">}</span></span>&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">val</span> secondProperty <span class="token operator">=</span> <span class="token string">&quot;Second property: <span class="token interpolation"><span class="token delimiter variable">${</span>name<span class="token punctuation">.</span>length<span class="token delimiter variable">}</span></span>&quot;</span><span class="token punctuation">.</span><span class="token function">also</span><span class="token punctuation">(</span><span class="token operator">::</span>println<span class="token punctuation">)</span>
    
    <span class="token keyword">init</span> <span class="token punctuation">{</span>
        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Second initializer block that prints <span class="token interpolation"><span class="token delimiter variable">${</span>name<span class="token punctuation">.</span>length<span class="token delimiter variable">}</span></span>&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre></div><p>类声明即加注解的主构造方法</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">class</span> Customer <span class="token keyword">public</span> <span class="token annotation builtin">@Inject</span> <span class="token keyword">constructor</span><span class="token punctuation">(</span>name<span class="token operator">:</span> String<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/*...*/</span> <span class="token punctuation">}</span>
</code></pre></div><p>备用构造方法</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">class</span> Person <span class="token punctuation">{</span>
    <span class="token keyword">var</span> children<span class="token operator">:</span> MutableList<span class="token operator">&lt;</span>Person<span class="token operator">&gt;</span> <span class="token operator">=</span> mutableListOf<span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">constructor</span><span class="token punctuation">(</span>parent<span class="token operator">:</span> Person<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        parent<span class="token punctuation">.</span>children<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token keyword">val</span> name<span class="token operator">:</span> String<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> children<span class="token operator">:</span> MutableList<span class="token operator">&lt;</span>Person<span class="token operator">&gt;</span> <span class="token operator">=</span> mutableListOf<span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">constructor</span><span class="token punctuation">(</span>name<span class="token operator">:</span> String<span class="token punctuation">,</span> parent<span class="token operator">:</span> Person<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        parent<span class="token punctuation">.</span>children<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>封闭构造方法</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">class</span> DontCreateMe <span class="token keyword">private</span> <span class="token keyword">constructor</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/*...*/</span> <span class="token punctuation">}</span>
</code></pre></div><p>开放继承，默认不能被继承</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">open</span> <span class="token keyword">class</span> Base <span class="token comment">//Class is open for inheritance</span>
</code></pre></div><p>构造父类</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">open</span> <span class="token keyword">class</span> <span class="token function">Base</span><span class="token punctuation">(</span>p<span class="token operator">:</span> Int<span class="token punctuation">)</span>

<span class="token keyword">class</span> <span class="token function">Derived</span><span class="token punctuation">(</span>p<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">Base</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span>
</code></pre></div><div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token comment">// 备用构造方法调用父类构造方法</span>
<span class="token keyword">class</span> MyView <span class="token operator">:</span> View <span class="token punctuation">{</span>
    <span class="token keyword">constructor</span><span class="token punctuation">(</span>ctx<span class="token operator">:</span> Context<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token keyword">super</span><span class="token punctuation">(</span>ctx<span class="token punctuation">)</span>

    <span class="token keyword">constructor</span><span class="token punctuation">(</span>ctx<span class="token operator">:</span> Context<span class="token punctuation">,</span> attrs<span class="token operator">:</span> AttributeSet<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token keyword">super</span><span class="token punctuation">(</span>ctx<span class="token punctuation">,</span> attrs<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>声明可重载</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">open</span> <span class="token keyword">class</span> Shape <span class="token punctuation">{</span>
    <span class="token keyword">open</span> <span class="token keyword">fun</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/*...*/</span> <span class="token punctuation">}</span>
    <span class="token keyword">fun</span> <span class="token function">fill</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/*...*/</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token function">Circle</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">Shape</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/*...*/</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>不能用val重载var</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">interface</span> Shape <span class="token punctuation">{</span>
    <span class="token keyword">val</span> vertexCount<span class="token operator">:</span> Int
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token function">Rectangle</span><span class="token punctuation">(</span><span class="token keyword">override</span> <span class="token keyword">val</span> vertexCount<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token operator">:</span> Shape <span class="token comment">// Always has 4 vertices</span>

<span class="token keyword">class</span> Polygon <span class="token operator">:</span> Shape <span class="token punctuation">{</span>
    <span class="token keyword">override</span> <span class="token keyword">var</span> vertexCount<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment">// Can be set to any number later</span>
<span class="token punctuation">}</span>
</code></pre></div><p>接口</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">interface</span> Shape <span class="token punctuation">{</span>
    <span class="token keyword">val</span> vertexCount<span class="token operator">:</span> Int
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token function">Rectangle</span><span class="token punctuation">(</span><span class="token keyword">override</span> <span class="token keyword">val</span> vertexCount<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token operator">:</span> Shape <span class="token comment">// Always has 4 vertices</span>

<span class="token keyword">class</span> Polygon <span class="token operator">:</span> Shape <span class="token punctuation">{</span>
    <span class="token keyword">override</span> <span class="token keyword">var</span> vertexCount<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment">// Can be set to any number later</span>
<span class="token punctuation">}</span>

</code></pre></div><p>内部类与外部类</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">class</span> FilledRectangle<span class="token operator">:</span> <span class="token function">Rectangle</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">fun</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/* ... */</span> <span class="token punctuation">}</span>
    <span class="token keyword">val</span> borderColor<span class="token operator">:</span> String <span class="token keyword">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;black&quot;</span>
    
    <span class="token keyword">inner</span> <span class="token keyword">class</span> Filler <span class="token punctuation">{</span>
        <span class="token keyword">fun</span> <span class="token function">fill</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/* ... */</span> <span class="token punctuation">}</span>
        <span class="token keyword">fun</span> <span class="token function">drawAndFill</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">super</span><span class="token label symbol">@FilledRectangle</span><span class="token punctuation">.</span><span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// Calls Rectangle's implementation of draw()</span>
            <span class="token function">fill</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Drawn a filled rectangle with color <span class="token interpolation"><span class="token delimiter variable">${</span><span class="token keyword">super</span><span class="token label symbol">@FilledRectangle</span><span class="token punctuation">.</span>borderColor<span class="token delimiter variable">}</span></span>&quot;</span><span class="token punctuation">)</span> <span class="token comment">// Uses Rectangle's implementation of borderColor's get()</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre></div><p>多继承</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">open</span> <span class="token keyword">class</span> Rectangle <span class="token punctuation">{</span>
    <span class="token keyword">open</span> <span class="token keyword">fun</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/* ... */</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">interface</span> Polygon <span class="token punctuation">{</span>
    <span class="token keyword">fun</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/* ... */</span> <span class="token punctuation">}</span> <span class="token comment">// interface members are 'open' by default</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token function">Square</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">Rectangle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> Polygon <span class="token punctuation">{</span>
    <span class="token comment">// The compiler requires draw() to be overridden:</span>
    <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token operator">&lt;</span>Rectangle<span class="token operator">&gt;</span><span class="token punctuation">.</span><span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// call to Rectangle.draw()</span>
        <span class="token keyword">super</span><span class="token operator">&lt;</span>Polygon<span class="token operator">&gt;</span><span class="token punctuation">.</span><span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// call to Polygon.draw()</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre></div><p>抽象类</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">open</span> <span class="token keyword">class</span> Polygon <span class="token punctuation">{</span>
    <span class="token keyword">open</span> <span class="token keyword">fun</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">abstract</span> <span class="token keyword">class</span> Rectangle <span class="token operator">:</span> <span class="token function">Polygon</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">abstract</span> <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

</code></pre></div><p>collections</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">fun</span> <span class="token function">printAll</span><span class="token punctuation">(</span>strings<span class="token operator">:</span> Collection<span class="token operator">&lt;</span>String<span class="token operator">&gt;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span>s <span class="token keyword">in</span> strings<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;<span class="token interpolation variable">$s</span> &quot;</span><span class="token punctuation">)</span>
        <span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    
<span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">val</span> stringList <span class="token operator">=</span> <span class="token function">listOf</span><span class="token punctuation">(</span><span class="token string">&quot;one&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;two&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;one&quot;</span><span class="token punctuation">)</span>
    <span class="token function">printAll</span><span class="token punctuation">(</span>stringList<span class="token punctuation">)</span>
    
    <span class="token keyword">val</span> stringSet <span class="token operator">=</span> <span class="token function">setOf</span><span class="token punctuation">(</span><span class="token string">&quot;one&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;two&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;three&quot;</span><span class="token punctuation">)</span>
    <span class="token function">printAll</span><span class="token punctuation">(</span>stringSet<span class="token punctuation">)</span>
</code></pre></div><p>mutable可变集合(支持读写)</p> <div class="language- extra-class"><pre class="language-text"><code>MutableListOf
MutableMapOf
MutableSetOf
</code></pre></div><p>增删查api</p> <div class="language- extra-class"><pre class="language-text"><code>.add
.remove
.contains
.count
</code></pre></div><p>Map默认是红黑树实现</p> <p>HashMap是散列表</p> <p>zip类似python的zip</p> <p>entries类似java的Map.Entry</p> <p>聚集运算</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">val</span> numbers <span class="token operator">=</span> <span class="token function">listOf</span><span class="token punctuation">(</span><span class="token string">&quot;one&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;two&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;three&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;four&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;five&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;six&quot;</span><span class="token punctuation">)</span>

<span class="token function">println</span><span class="token punctuation">(</span>numbers<span class="token punctuation">.</span><span class="token function">groupingBy</span> <span class="token punctuation">{</span> it<span class="token punctuation">.</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">.</span><span class="token function">eachCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><p>别名</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">typealias</span> NodeSet <span class="token operator">=</span> Set<span class="token operator">&lt;</span>Network<span class="token punctuation">.</span>Node<span class="token operator">&gt;</span>
</code></pre></div><p>安全类型</p> <p>默认不允许为null</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">var</span> a<span class="token operator">:</span> String <span class="token operator">=</span> <span class="token string">&quot;abc&quot;</span> <span class="token comment">// Regular initialization means non-null by default</span>
a <span class="token operator">=</span> <span class="token keyword">null</span> <span class="token comment">// compilation error</span>
</code></pre></div><p>问号允许为null</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">var</span> a<span class="token operator">:</span> String<span class="token operator">?</span> <span class="token operator">=</span> <span class="token string">&quot;abc&quot;</span> <span class="token comment">// Regular initialization means non-null by default</span>
a <span class="token operator">=</span> <span class="token keyword">null</span> <span class="token comment">// ok</span>
</code></pre></div><p>强制非空</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">val</span> l <span class="token operator">=</span> b<span class="token operator">!!</span><span class="token punctuation">.</span>length<span class="token comment">// 如果b为空则抛出异常</span>
</code></pre></div><p>安全转换</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">val</span> aInt<span class="token operator">:</span> Int<span class="token operator">?</span> <span class="token operator">=</span> a <span class="token keyword">as</span><span class="token operator">?</span> Int<span class="token comment">//如果转换失败则为null</span>
</code></pre></div><p>过滤可空集合的空元素</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">val</span> nullableList<span class="token operator">:</span> List<span class="token operator">&lt;</span>Int<span class="token operator">?</span><span class="token operator">&gt;</span> <span class="token operator">=</span> <span class="token function">listOf</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span>

<span class="token keyword">val</span> intList<span class="token operator">:</span> List<span class="token operator">&lt;</span>Int<span class="token operator">&gt;</span> <span class="token operator">=</span> nullableList<span class="token punctuation">.</span><span class="token function">filterNotNull</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div></div> <footer class="page-edit"><!----> <!----> <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh"><img alt="知识共享许可协议" src="" style="border-width:0"></a><br>本作品采用<a rel="license" href="http://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>进行许可。

   
</footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/知识点总结/js总结.html" class="prev">
        js易错点
      </a></span> <span class="next"><a href="/知识点总结/latex总结.html">
        latex常用代码
      </a>
      →
    </span></p></div>  <footer style="text-align:center;"><a href="https://beian.miit.gov.cn">
    粤ICP备2021020303号</a></footer></main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.54ea76a2.js" defer></script><script src="/assets/js/2.c554ece5.js" defer></script><script src="/assets/js/37.61156e90.js" defer></script>
  </body>
</html>
